<template>
  <div class="search-section">
    <a-row :gutter="[16, 16]">
      <a-col :span="12">
        <a-card title="🔍 搜索热词统计" class="search-card">
          <template #extra>
            <a-button type="text" size="small">
              <template #icon><i class="icon icon-refresh"></i></template>
              刷新
            </a-button>
          </template>
          <div class="word-cloud-container">
            <Chart :option="wordCloudOption" height="300px" />
          </div>
        </a-card>
      </a-col>
      <a-col :span="12">
        <a-card title="📊 搜索转化分析" class="search-card">
          <div class="search-stats">
            <div class="search-stat-item">
              <div class="stat-value">{{ searchStats.totalSearches }}</div>
              <div class="stat-label">总搜索次数</div>
            </div>
            <div class="search-stat-item">
              <div class="stat-value">{{ searchStats.successRate }}%</div>
              <div class="stat-label">搜索成功率</div>
            </div>
            <div class="search-stat-item">
              <div class="stat-value">{{ searchStats.clickRate }}%</div>
              <div class="stat-label">点击转化率</div>
            </div>
            <div class="search-stat-item">
              <div class="stat-value">{{ searchStats.avgResults }}</div>
              <div class="stat-label">平均结果数</div>
            </div>
          </div>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useChartOption from '@/hooks/chart-option';
  import Chart from '@/components/chart/index.vue';
  import 'echarts-wordcloud';

  // 搜索关键词
  const searchKeywords = ref([
    { word: 'Vue3', count: 156, level: 1 },
    { word: 'Spring Boot', count: 134, level: 1 },
    { word: 'JavaScript', count: 98, level: 2 },
    { word: 'React', count: 87, level: 2 },
    { word: '算法', count: 76, level: 2 },
    { word: '数据结构', count: 65, level: 3 },
    { word: 'TypeScript', count: 54, level: 3 },
    { word: '微服务', count: 43, level: 3 },
    { word: 'Hooks', count: 38, level: 3 },
    { word: '异步编程', count: 32, level: 3 },
    { word: 'Node.js', count: 28, level: 3 },
    { word: 'Docker', count: 25, level: 3 },
    { word: 'Redis', count: 22, level: 3 },
    { word: 'MySQL', count: 20, level: 3 },
    { word: 'Webpack', count: 18, level: 3 },
    { word: '前端', count: 45, level: 2 },
    { word: '后端', count: 38, level: 3 },
    { word: '全栈', count: 15, level: 3 },
  ]);

  // 搜索统计
  const searchStats = ref({
    totalSearches: 1247,
    successRate: 89.6,
    clickRate: 67.8,
    avgResults: 3.2,
  });

  // 词云图表配置
  const { chartOption: wordCloudOption } = useChartOption(() => ({
    backgroundColor: 'transparent',
    tooltip: {
      show: true,
      backgroundColor: 'rgba(50, 50, 50, 0.9)',
      borderColor: 'rgba(255, 255, 255, 0.2)',
      borderWidth: 1,
      textStyle: {
        color: '#fff',
        fontSize: 12,
      },
      formatter: (params: any) => {
        return `<strong style="color: #fff">${params.name}</strong><br/>搜索次数: <span style="color: #64b5f6">${params.value}</span>`;
      },
    },
    series: [
      {
        type: 'wordCloud',
        gridSize: 8,
        sizeRange: [16, 48],
        rotationRange: [-90, 90],
        rotationStep: 45,
        shape: 'circle',
        width: '100%',
        height: '100%',
        drawOutOfBound: false,
        textStyle: {
          fontFamily: 'Microsoft YaHei, sans-serif',
          fontWeight: 'normal',
        },
        emphasis: {
          focus: 'self',
        },
        data: searchKeywords.value.map((item) => {
          const getColorByLevel = (level: number) => {
            if (level === 1) return '#667eea';
            if (level === 2) return '#11998e';
            return '#fcb045';
          };

          return {
            name: item.word,
            value: item.count,
            textStyle: {
              color: getColorByLevel(item.level),
              fontWeight: item.level === 1 ? 'bold' : 'normal',
            },
          };
        }),
      },
    ],
  }));
</script>

<style lang="less" scoped>
  .search-section {
    margin-bottom: 32px;

    .search-card {
      border-radius: 20px;
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(20px);
      border: 1px solid rgba(255, 255, 255, 0.3);
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);

      :deep(.arco-card-header) {
        border-bottom: 1px solid rgba(0, 0, 0, 0.06);
        padding: 20px 24px 16px;

        .arco-card-header-title {
          font-size: 16px;
          font-weight: 600;
          color: #2d3748;
        }
      }

      .word-cloud-container {
        width: 100%;
        min-height: 300px;
        display: flex;
        align-items: center;
        justify-content: center;

        :deep(.echarts) {
          background: transparent;
        }
      }

      .search-stats {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 16px;

        .search-stat-item {
          text-align: center;
          padding: 20px;
          background: rgba(255, 255, 255, 0.6);
          border: 1px solid rgba(255, 255, 255, 0.2);
          border-radius: 16px;
          transition: all 0.3s ease;

          &:hover {
            background: rgba(255, 255, 255, 0.8);
            transform: translateY(-2px);
          }

          .stat-value {
            font-size: 28px;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 6px;
          }

          .stat-label {
            font-size: 13px;
            color: #718096;
            font-weight: 500;
          }
        }
      }
    }
  }

  // 响应式适配
  @media (max-width: 1200px) {
    .search-section {
      margin-bottom: 24px;
    }
  }

  @media (max-width: 768px) {
    .search-section {
      margin-bottom: 20px;

      .search-card {
        border-radius: 16px;

        .word-cloud-container {
          min-height: 200px;

          :deep(.chart-container) {
            height: 200px !important;
          }
        }

        .search-stats {
          gap: 12px;

          .search-stat-item {
            padding: 16px;

            .stat-value {
              font-size: 22px;
            }

            .stat-label {
              font-size: 11px;
            }
          }
        }
      }
    }
  }
</style>
